<?php
class upload_file{
	private $uploaddir; //文件上传存路径
	private $fileform = array(); //顺序文件表单name数组array('file1','file2')
	private $max_files = 10; //一次性最多上传多少文件
	private $max_size = 2048000; //文件最大
	private $permission = 0777; //文件夹是否可以有权限
	private $allowed = array(); //允许上传文件格式
	private $notallowed = array("exe", "php", "html", "js"); //不允许上传文件格式
	private $imagemaxwidth = 600;//图片最大宽度，超过就截取为600
	private $imagemaxheight = 600;//图片最大高度，超过就截取为600
	private $if_image = true;//是否是图片
	private $if_mini = true;//是否生成缩略图
	public  $thumbwidth = 100; //生成缩略图宽度
	public  $thumbheight = 100; //生成缩略图高度
	private $filedir;//文件存储目录,默认$uploaddir/date('Ym');
	public $filearray = array();//顺序返回一堆文件地址数组array(array('max.jpg','min.jpg'),array('max.jpg','min.jpg'))
	public $error = '';
	
	function __construct($uploaddir = '',$fileform = array(),$if_mini = true){
		if($uploaddir != '')	$this->uploaddir = $uploaddir;
		if(!empty($fileform))	$this->fileform = $fileform;
		$this->if_mini = $if_mini;
		
		if(empty($this->fileform)){
			$this->error .= '上传文件为空';
			return false;
		}else if(count($this->fileform) > $this->max_files){
			$this->error .= '上传文件总数超过限制';
			return false;
		}
		if(!is_dir($this->uploaddir) && !mkdir($this->uploaddir,$this->permission)){
			$this->error .= $this->uploaddir."这个目录不存在，并且无法创建";
			return false;
		}
		$this->filedir = $this->uploaddir.'/'.date('Ym');
		if(!is_dir($this->filedir) && !mkdir($this->filedir,$this->permission)){
			$this->error .= $this->filedir."这个目录不存在，并且无法创建";
			return false;
		}
		
	}
	
	public function move_upload(){
		for($i = 0;$i < count($this->fileform);$i++ ){
			if($_FILES[$this->fileform[$i]]['size'] > $this->max_size){
				$this->error .= "文件".$_FILES[$this->fileform[$i]]['name']."超过大小限制";
				return false;
			}elseif(!$this->check_pictype($_FILES[$this->fileform[$i]]['name'])){
				$this->error .= "文件".$_FILES[$this->fileform[$i]]['name']."为非法上传文件";
				return false;
			}
		}
		if ($this->if_image){//是图片
			for($i = 0;$i < count($this->fileform);$i++ ){
				$tmpname = $this->random(5).'.'.$this->get_fileext($_FILES[$this->fileform[$i]]['name']);
				$maximg_addr = $this->filedir.'/max'.$tmpname;	
				if(!move_uploaded_file($_FILES[$this->fileform[$i]]['tmp_name'],$maximg_addr)){
					$this->error .= "文件".$_FILES[$this->fileform[$i]]['name']."上传过程出错";
					return false;
				}
				if($this->if_mini){
					$tmpaddr = $this->thumb_image($maximg_addr,$tmpname);
					if(!$tmpaddr){
						$this->error .= '文件'.$maximg_addr.'生成缩略图发生错误';
						return false;
					}
					$minimg_addr = $this->thumb_image($maximg_addr,$tmpname);
					array_push($this->filearray, array($maximg_addr,$minimg_addr));
				}else{
					array_push($this->filearray, array($maximg_addr));
				}
				
			}
				
		}else{
			for($i = 0; $i < count($this->fileform); $i++ ){
				$tmpname = $this->random(8).'.'.$this->get_fileext($_FILES[$this->fileform[$i]]['name']);
				$maxfile_addr = $this->filedir.'/'.$tmpname;
				move_uploaded_file($_FILES[$this->fileform[$i]]['tmp_name'],$maxfile_addr);
				array_push($this->filearray, array($maxfile_addr));
			}
		}	
		return true;
	}
	
	private function check_pictype($filename){	
		$fileext=$this->get_fileext($filename,true);
		if(empty($this->allowed) && in_array($fileext,$this->notallowed)){
			return false;
		}else{
			return true;
		}
		if(!empty($this->allowed) && !in_array($fileext,$this->notallowed) && in_array($fileext,$this->allowed)){
			return true;
		}else{
			return false;	
		}
	}
	
	private function get_fileext($filename,$tolower = true){
		$fileext=substr(strrchr($filename, '.'),1);
		return $tolower ? strtolower($fileext) : $fileext;
	}
	private function random($length){
		$hash=""; 
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 
		$max=strlen($chars)-1; 
		mt_srand((double)microtime() * 1000000); 
		for($i=0;$i<$length;$i++){ 
			$hash.=$chars[mt_rand(0,$max)]; 
		}
		return $hash; 
	}
	
	private function thumb_image($img_addr,$tmpname){
		if(!file_exists($img_addr)){
			$this->error .= '要生成缩略图的原图不存在';
			return false;
		}
		$tmparr = getimagesize($img_addr);
		$imagewidth = $tmparr[0];
		$imageheight = $tmparr[1];
		if($imagewidth < $this->thumbwidth && $imageheight < $this->thumbheight){
			return $img_addr;
		}
		switch ($tmparr['mime']){
			case 'image/jpeg':
				$imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
				$imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
				break;
			case 'image/png':
				$imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
				$imagefunc = function_exists('imagepng') ? 'imagepng' : '';
				break;
			case 'image/gif':
				$imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
				$imagefunc = function_exists('imagegif') ? 'imagegif' : '';
				break;
			default:
				return false;
				break;	
		}
		if(function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')){
			if(($imagewidth >= $this->thumbwidth || $imageheight >= $this->thumbheight)){
				$attach_photo = $imagecreatefromfunc($img_addr);
				$x_ratio = $this->thumbwidth / $imagewidth;
				$y_ratio = $this->thumbheight / $imageheight;
				if(($x_ratio * $imageheight) < $this->thumbheight){
					$thumb['height'] = ceil($x_ratio * $imageheight);
					$thumb['width'] = $this->thumbwidth;
				}
				else{
					$thumb['width'] = ceil($y_ratio * $imagewidth);
					$thumb['height'] = $this->thumbheight;
				}
				$targetfile = $this->filedir.'/min'.$tmpname;
				$thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
				imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
				if($tmparr['mime'] == 'image/jpeg'){
					$imagefunc($thumb_photo, $targetfile, 100);
				}else{
					$imagefunc($thumb_photo, $targetfile);
				}
			}
		}else{
			return false;
		}
		if(file_exists($targetfile)){
			return $targetfile;
		}else{
			return false;
		}
	}
	
}

?>